import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

public class test_threadlocal {

    private static final AtomicInteger nextId = new AtomicInteger(0);

    private static final ThreadLocal<Integer> threadId = new ThreadLocal<Integer>(){
        @Override
        protected Integer initialValue(){
            return nextId.getAndIncrement();
        }
    };

    private static int get(){
        return threadId.get();
    }


    public static void main(String[] args) {

        ExecutorService es = Executors.newCachedThreadPool();

        for (int i = 0; i < 5; i++){
            es.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println("threadName = " + Thread.currentThread().getName() + ", threadId=" + get());
                }
            });
        }

    }

}
